local skynet = require "skynet"
local msghelper = require "tablestatesvrhelper"
local base = require "base"
local serverbase = require "serverbase"
local table = table
require "enum"
require "skynet.manager"

local params = ...

--构建和桌子人数对应的索引
local function build_tableplayernum_indexs()
	--[=[
		[gameid] = {
			[room_type] = {
					1,2,...10 指人数，10表示空
				    [1]={
						--[[
							id=true,
						]]
					},
					[2]={},
					……
					[5]={},
			},	
		},
	]=]

	local t = {}
	for i=1, EGameId.GAME_ID_MAX do
		t[i] = {}		
	end

	local tmp
	for _, value in ipairs(t)  do
		for i=1, ERoomType.ROOM_TYPE_MAX do
			value[i] = {}
			tmp = value[i]
			for j=1, 10 do
				tmp[j]={}
			end
		end				
	end
	return t
end

local TableStatesvrd = serverbase:new({
	table_pool = {
		--[[
			[id]={
				optional int32 id = 1;     //table id
				optional int32 state = 2;  //table state
				optional string name = 3;  //桌子名字
				optional int32 gameid = 4; //游戏id
				optional int32 room_type = 5; //房间类型
				optional int32 max_player_num = 6;   //房间支持的最大人数
				optional int32 cur_player_num = 7;   //状态服务器
				optional int32 retain_to_num = 8;    //游戏保留局数
				optional int32 curr_to_num = 9;     //游戏当前已经进行了的局数
				optional int32 retain_to_time =10;    //桌子保留到的时间(linux时间擢)
				optional int32  create_user_rid = 11;  //创建者rid
				optional string create_user_rolename = 12; //创建者姓名
				optional string create_user_logo = 13;
				optional int32  create_time = 14;      //桌子的创建时间
				optional string create_table_id = 15;  //创建桌子的索引id	
			    optional int32 action_timeout = 16;
			    optional int32 action_timeout_count = 17;     

				optional string roomsvr_id = 18;       //房间服务器id
				optional int32 roomsvr_table_address = 19; //桌子table的地址
				optional int32 totalplayernum = 20;       //在场次列表中显示在线人数
				optional int32 distribute_playernum = 21;
				optional bool is_need_password = 22;      //是否需要密码
				//牛牛
				optional NNTableStateCfg nngamecfg = 23;	
		}
		]]
	},

	create_table_indexs = {
		--[[
			[gameid]={
				[create_table_id] = id	
			}
		]]
	},

	--游戏id, 服务器serverid、房间类型、游戏类型相关的索引
	roomsvrs = {
	--[[gamid = {
		roomsvr_id = {
			room_type = {
				num=0,		--累计在线人数
				id1,
				id2
			},
			update_time = 0,
		},	
	},
	]]
	},

	--根据创建者建立桌子的索引状态
	createusers_table_indexs = {
		--[[
			rid={
				[gameid]={
					id=true
				}				
			}
		]]
	},

	--和桌子人数对应的索引
	tableplayernumindexs = build_tableplayernum_indexs(),
})

function TableStatesvrd:tostring()
	return "TableStatesvrd"
end

local function tablestatesvrd_to_sring()
	return TableStatesvrd:tostring()
end

function  TableStatesvrd:init()
	msghelper:init(TableStatesvrd)
	self.eventmng.init(TableStatesvrd)
	self.eventmng.add_eventbyname("cmd", "tablestatesvrcmd")
	self.eventmng.add_eventbyname("notice", "tablestatesvrnotice")
	self.eventmng.add_eventbyname("request", "tablestatesvrrequest")
	TableStatesvrd.__tostring = tablestatesvrd_to_sring
end 

skynet.start(function()  
	if params == nil then
		TableStatesvrd:start()
	else		
		TableStatesvrd:start(table.unpack(base.strsplit(params, ",")))
	end	
end)
